// packagBuy/pages/commonProblem/questionDetail/questionDetail.js
var util = require('../../../../utils/util')   //引入配置公共请求接口
import {
  foundApi
} from '../../../../config/config'
var configUrl = require('../../../../config/config') //引入配置接口文件
//获取应用实例
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    footprintGuid: '', //足迹全局id
    detailData: [],
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    /**-合伙人--*/
    isShowPartnerAll: 1,
    showPartnerCodePop: false,
    statusBarHeight: app.globalData.statusHeight
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this
    if (!wx.getStorageSync('addrCode').provinceCode) {
      app.getUserLocation() //初始化获取定位地址
    }
    if (!wx.getStorageSync('loginMsg').token) {
      app.initLogin() //初始化登录
    }
    console.log(options)
    if (options.hid) {
      var scene = decodeURIComponent(options.hid) //参数二维码传递过来的参数
      var stInFun = setInterval(() => {
        if (wx.getStorageSync('loginMsg').token) {
          clearInterval(stInFun)
          if (!wx.getStorageSync('loginMsg').loginState) {
            if (wx.getUserProfile) {
              app.getUserProfile(function(){
                that.setData({
                  getPhoneNumberData:{
                    isShowGetPhoneNumber:true
                  }
                })
              })
            } else {
              if(!wx.getStorageSync('loginMsg').bindingPhone){
                that.setData({
                  getPhoneNumberData:{
                    isShowGetPhoneNumber:true
                  }
                })
              }
            }
            // this.setData({
            //   isShowGetUserInfo: true
            // })
          }
          app.bindingPartner(scene)
          this.setData({
            id: options.id,
            allTitle:options.name
          })
          this.gethelpDetail()
        }
      }, 1000)
      console.log('合伙人分享')
    } else if (options.option) {
      let option = JSON.parse(options.option)
      // wx.setNavigationBarTitle({
      //   title: option.name
      // })
      this.setData({
        id: option.id,
        allTitle:option.name
      })
      var stInFun = setInterval(() => {
        if (wx.getStorageSync('loginMsg').token) {
          clearInterval(stInFun)
          this.gethelpDetail()
        }
      }, 1000)
    }
  },
  bindGetUserInfo: function (e) { //登录-注册
    var that=this;
    app.getUserInfo()
    this.setData({
      showSetting: false,
      isShowGetUserInfo: false
    })
    var setFun=setInterval(()=>{
      if(wx.getStorageSync('showGetPhone')){
        that.setData({
          getPhoneNumberData:{
            isShowGetPhoneNumber:true, 
          },
          showSetting: false,
          isShowGetUserInfo: false
        })
        wx.removeStorageSync('showGetPhone')
        clearInterval(setFun)
      }
    },1000)
    
  },
  onCloseShowGetUserInfo() { //关闭弹出层
    this.setData({
      showSetting: false,
      isShowGetUserInfo: false
    })
  },
   /**绑定手机号 ---str*/
   closeGetphone(){
    this.setData({
      getPhoneNumberData:{
        isShowGetPhoneNumber:false

      }
    })
  },
  getPhoneNumber(e){
    var that=this;
    let loginMsg = wx.getStorageSync('loginMsg')
    if (e.detail.iv && e.detail.encryptedData) {
      let option = {}
      option.encryptedData = e.detail.encryptedData
      option.iv = e.detail.iv
      option.openid = loginMsg.openid
      option.session_key = loginMsg.session_key
      app.reqFun.requestJson('POST',configUrl.decryptGetPhone,option,(res)=>{
        if(res.data.success==1){
          that.setData({
            getPhoneNumberData:{
              isShowGetPhoneNumber:false,
            }
          })
          wx.showToast({
            title: '绑定成功。',
          })
        }
      }) 
    } else {
      console.log('取消')
    }
  },
  /**  end
   *  绑定手机号
   */
  gethelpDetail() {
    let helpDetailUrl = foundApi.helpDetail + '/' + this.data.id
    app.reqFun.requestJson('get', helpDetailUrl, '', (res) => {
      if (res.data.success == 1) {
        let result = res.data.data.content
        const regex = new RegExp('<img', 'gi');
        result = result.replace(regex, `<img style="width: 90%;margin:0 auto;"`);
        this.setData({
          detailData: res.data.data,
          html: result
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      footprintGuid: util.guid()
    })
    setTimeout(()=>{
      this.setData({
        partnerInfo:getApp().globalData.partnerInfo
      })
      },500)
    if (this.data.id) {
      //保存访问足迹
      app.saveAccessFootprint(this.data.footprintGuid, 'content', this.data.id)
    } else {
      //拿到资源id再记录足迹
      var footprintWaitResourceIdTimer = setInterval(()=>{
        if(this.data.id){
          //保存访问足迹
          app.saveAccessFootprint(this.data.footprintGuid, 'content', this.data.id)
          clearInterval(footprintWaitResourceIdTimer)
        }
      }, 100)
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    //保存退出足迹
    app.saveQuitFootprint(this.data.footprintGuid)
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    //保存退出足迹
    app.saveQuitFootprint(this.data.footprintGuid)
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },
  goHome: function (e) { //返回主页
    wx.reLaunch({
      url: '/pages/newIndex/newIndex'
    })
  },
  goBack(e){
    wx.navigateBack({
      delta: 1,
    })
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    let path = `/packagBuy/pages/commonProblem/questionDetail/questionDetail?id=${this.data.id}&name=${this.data.allTitle}&hid=${wx.getStorageSync('loginMsg').userId}`
    return {
      title: this.data.allTitle,
      desc: this.data.allTitle,
      path: path
     }
  },
    //展开合伙人信息
    showPartnerAll: function () {
      let isShowPartnerAll;
      if (this.data.isShowPartnerAll == 1) {
        isShowPartnerAll = 0;
      } else {
        isShowPartnerAll = 1;
      }
      this.setData({
        isShowPartnerAll: isShowPartnerAll,
      })
    },
    //展示合伙人二维码
    partnerCode: function (e) {
      // this.setData({
      //   showPartnerCodePop: true,
      // })
      let codeImg=e.target.dataset.url
      wx.previewImage({
        urls:[codeImg],
        current: codeImg, // 当前显示图片的http链接
       })
    },
    //关闭合伙人二维码
    closeCodePop: function () {
      console.log("3253456678dsgudkdkgfksfvkjdsvbjkdsvbvkj")
      this.setData({
        showPartnerCodePop: false,
      })
    },
    //拨打合伙人电话
    partnerPhone: function () {
      let that = this;
      wx.makePhoneCall({
        phoneNumber: that.data.partnerInfo.partnerPhone,
      })
    }
})